package hotciv.common;

import hotciv.framework.City;
import hotciv.framework.GameConstants;
import hotciv.framework.Player;
import hotciv.framework.Position;
import hotciv.framework.Tile;
import hotciv.framework.TileMap;
import hotciv.framework.Unit;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;

public class WorldMap {
	
	private TileMap tileMap;  
	private HashMap<Position, Unit> unitMap;
	private HashMap<Position, City> cityMap;
	

	
	public WorldMap(TileMap tileMap, HashMap<Position, Unit> unitMap, HashMap<Position, City> cityMap) {
		this.tileMap = tileMap;  
		this.unitMap = unitMap;
		this.cityMap = cityMap;
	}
	
	/** Set the unit at position p.
	   * Precondition: it is possible to place a unit at location 'p'.
	   * @param p the position that the unit must be placed in
	   * @param u the unit to be placed
	   */
	public Unit getUnitAt(Position p) {
		return unitMap.get(p);
	}

	/** Set the city at position p.
	   * Precondition: it is possible to place a city at location 'p'.
	   * @param p the position that the city must be placed in
	   * @param c the city to be placed
	   */
	public City getCityAt(Position p) {
		return cityMap.get(p);
	}

	/** Set the tile at position p.
	   * Precondition: position 'p' is within range of world.
	   * @param p the position that the tile must be placed in
	   * @param t the tile to be placed
	   */
	public Tile getTileAt(Position p) {
		return tileMap.getTileAt(p);
	}

	public void setUnitAt(Position p, Unit u) {
		unitMap.put(p, u);
	}

	public void setCityAt(Position p, City c) {
		cityMap.put(p, c);
	}
	
	public void removeUnitAt(Position p) {
		setUnitAt(p, null);
	}
	
	public Position getCityPosition(City city) {
		if (cityMap.containsValue(city)) {
			  Position cPos = null;
			  for(Entry<Position, City> e : cityMap.entrySet()) {
				  if (e.getValue().equals(city)) {
					  cPos = e.getKey();
					  break;
				  }
			  }
			  return cPos;
		  } else return null;
	}
	
	public Collection<City> getAllCities() {
		return cityMap.values();
	}

}
